/**
 * User: jhumphrey
 * Date: Oct 22, 2007
 */

import net.java.io.FileTools;

String basedir = properties["basedir"];
String projectName = properties["project.name"];
String filePath = basedir + "/" + projectName + ".iml";
String imlContents = FileTools.read(filePath).toString();

// if the intellij iml file doesn't have the 'classpath="eclipse" attribute within the module element, then
// add it and then write out the eml file to the project root
if ( !(imlContents =~ /<module.*classpath="eclipse"/)) {
  System.out.println("Upgrading project to IntelliJ 7.0 to use eclipse .classpath...");
  // find the '<module' and replace it with '<module classpath="eclipse"'
  String stringToFind = "<module";
  String firstString = imlContents.substring(0, imlContents.indexOf(stringToFind) + stringToFind.length());
  String inputString = " classpath=\"eclipse\"";
  String lastString = imlContents.substring(firstString.length(), imlContents.length());
  String finalString = firstString + inputString + lastString;

  // write out the new file to the project root
  FileTools.write(new File(filePath), finalString);

  // now write out the eml file
  FileTools.copy(properties["dir.plugin.ide"] + "/files/project.eml", basedir + "/" + projectName + ".eml");
}